package com.foton.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 简介说明: Spring工厂工具类封装
 *
 * @author: RenPu
 * @date: 2024/11/03 11:16:14
 * @version: 1.0
 */

@Component
public class SpringFactoryUtil implements ApplicationContextAware {


    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       this.applicationContext = applicationContext;
    }

    /**
     * @param clazz
     * @return
     * @param <T>
     * 根据Bean的名称，获取对应的Bean实例对象
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }


    /**
     * @param beanName
     * @param clazz
     * @return
     * @param <T>
     * 根据Bean的名车+Bean得Class类型，俩个维度进行获取对应的Bean实例对象
     */
    public static <T> T getBean(String beanName,Class<T> clazz) {
        return applicationContext.getBean(beanName,clazz);
    }
}